package com.groupbuying.customerconsumer.mq;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.groupbuying.customerconsumer.constants.RedisConstants;
import com.groupbuying.customerconsumer.dao.CustomerAliinfoRelDao;
import com.groupbuying.customerconsumer.dao.CustomerUshopDao;
import com.groupbuying.customerconsumer.domain.CustomerAliinfoRelDO;
import com.groupbuying.customerconsumer.domain.CustomerUshopDO;
import com.groupbuying.customerconsumer.utils.RedisUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 *  支付宝注册信息
 * @auther lisurilige
 * @date 2019/8/26
 */
@Component
public class CustomerAlipay {

    Logger logger = LoggerFactory.getLogger(CustomerAlipay.class);

    @Autowired
    CustomerUshopDao customerUshopDao;

    @Autowired
    private CustomerAliinfoRelDao customerAliinfoRelDao;

    @Autowired
    private RedisUtils redisUtils;

    public boolean saveAlipayRegisterObject(Map<String, Object> messageBody){

        logger.info("支付宝注册信息====msg={}", messageBody);

        //判断map是否有数据
        if (null == messageBody || messageBody.size() <= 0) {
            logger.error("支付宝注册信息为空");
            return false;
        }

        CustomerUshopDO customer = JSON.toJavaObject((JSON) messageBody.get("customer"),CustomerUshopDO.class);
        CustomerAliinfoRelDO alipayRel = JSON.toJavaObject((JSON) messageBody.get("alipayRel"),CustomerAliinfoRelDO.class);
        logger.info("支付宝注册信息，用户表参数信息 customer:{}",customer);
        logger.info("支付宝注册信息，支付宝关联表参数信息 aliRel:{}",alipayRel);

        //判断是否为空
        if (customer == null || alipayRel == null) {
            logger.error("支付宝注册信息不存在");
            return true;
        }
        //判断主键是否为空字符串
        if (StringUtils.isEmpty(customer.getCustomerId())){
            logger.error("customerId 不能为空");
            return true;
        }

        String userId = alipayRel.getAlipayUserid()==null?null:alipayRel.getAlipayUserid();

        //判断支付宝userId是否为空
        if (StringUtils.isEmpty(userId)){
            logger.error("customer消费者，支付宝注册信息保存 userId 不能为空");
            return true;
        }

        //没有redis信息-保存 有redis信息 返回true 为了防止重复提交
        String redisKey = RedisConstants.RDS_ALI_USERID+userId;
        if (redisUtils.isHasKey(redisKey)){
            logger.error("customer消费者，支付宝注册信息保存 userid 重复提交");
            return true;
        }
        boolean isTrue = redisUtils.setIfAbsent(redisKey,userId,RedisConstants.RDS_ALI_EXP);

        //redis  保存不成功  重新消费
        if (!isTrue){
            logger.error("customer消费者，支付宝注册信息保存 userid保存redis有误");
            return false;
        }

        //执行保存方法
        try {
            customerUshopDao.save(customer);
            customerAliinfoRelDao.save(alipayRel);
        } catch (DuplicateKeyException d) {
            logger.info("提示信息：{}，customerId：{}","支付宝注册信息，主键冲突,当前数据已经保存多次",customer.getCustomerId());
            return true;
        } catch (Exception e) {
            logger.info("提示信息：{}，customer：{}aliRel：{}","支付宝注册信息，保存数据其他异常",customer,alipayRel);
            return true;
        }

        return true;
    }
}
